<template>
    <div>
        <el-form ref="dataSourceForm" label-position="left" label-width="150px" >
            <el-form-item label="数据来源名称">
                <el-input v-model="dataSource.data_source_name" style="width:500px;"></el-input>
            </el-form-item>
            <el-form-item label="数据来源别名">
                <el-input v-model="dataSource.data_source_alias" style="width:400px;"></el-input>
            </el-form-item>
            <el-form-item label="数据来源权威度">
                <el-input-number v-model="dataSource.data_source_degree" style="width:200px;" :min = "0" :max = "10" ></el-input-number>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :disabled="submitLoading" :loading="submitLoading" @click="submitForm">保存</el-button>
                <!--<el-button>取消</el-button>-->
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { getDataSourceById, saveDataSource } from '../../api/crawl.js';
import lodash from 'lodash';
export default {
    data(){
        return {
            dataSourceForm : {
            },
            submitLoading : false,
            dataSource : {
                data_source_id : 0,
                data_source_name : '',
                data_source_alias:'',
                data_source_degree : 3,
            },
        }
    },
    components: {
    },
    created(){
        const data_source_id = this.$route.params.data_source_id;
        if(Number(data_source_id)){
            this.dataSource.data_source_id = data_source_id;
            this.initDataSourceById();
        }else{
            this.dataSource.data_source_id = 0;
        }
    },
    watch: {
    },
    methods: {
        initDataSourceById(){
            getDataSourceById(this.dataSource.data_source_id).then(data=>{
                console.log('getDataSourceById------------', data);
                this.dataSource = data.rows[0];
            })
        },
        async submitForm(){
            try{
                //保存数据
                const postData = Object.assign({}, this.dataSource);
                this.submitLoading = true;
                const res = await saveDataSource(postData);
                this.submitLoading = false;
                console.log('saveDataSource------',res)
                if(res){
                    if (res.data_source_id) {
                        this.$alert('新增成功！', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.$router.push('/dataSourceList');
                            }
                        });
                    }else if (res === 1) {
                        this.$alert('修改成功！', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.$router.push('/dataSourceList');
                            }
                        });
                    }
                }else{
                    this.$message({
                        message: '保存失败！',
                        type: 'danger'
                    });
                }
            }catch(e){
                console.error(e);
            }
        }
    }
};
</script>